<template>
  <div id="uesrtext">
    <textarea placeholder="按 Ctrl + Enter 发送" v-model="content" v-on:keyup="addMessage"></textarea>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'uesrtext',
  data() {
    return {
      content: '',
    }
  },
  computed: mapState(['currentSession']),
  methods: {
    addMessage(e) {
      if (e.ctrlKey && e.keyCode === 13 && this.content.length) {
        console.log(this.currentSession)
        let msg = new Object()
        msg.to = this.currentSession.username // 发送给谁
        msg.content = this.content // 聊天内容
        this.$store.state.stomp.send('/ws/chat', {}, JSON.stringify(msg)) // 将消息发送到后端接口
        this.$store.commit('addMessage', msg) // 添加消息聊天记录
        this.content = '' // 发完就清空
      }
    },
  },
}
</script>

<style lang="scss" scoped>
#uesrtext {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 100%;
  height: 30%;
  border-top: solid 1px #ddd;
  > textarea {
    padding: 10px;
    width: 100%;
    height: 100%;
    border: none;
    outline: none;
  }
}
</style>
